package com.lebang.common.validcode;

import com.lebang.entity.SdkVideoBaidu;
import com.lebang.model.ValidCodeRecord;
import com.lebang.model.back.ImeiRegister;
import com.lebang.service.mongodb.ValidCodeMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author: hwq
 * @Date: 2020/1/9 10:51
 * @Version 1.0
 */
@Service
public class ValidCodeRecordService {
    @Autowired
    private ValidCodeMongodb validCodeMongodb;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Async
    public void insertRecord(ValidCodeRecord record){
        try {
            record.setCreatTime(LocalDateTime.now());
            validCodeMongodb.save(record);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 统计时间段内的短信发送量
     * @param star
     * @param end
     * @return
     */
    public Long countValidNum(LocalDateTime star, LocalDateTime end) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("creatTime").gte(star).lte(end);
        query.addCriteria(criteria);
        return mongoTemplate.count(query, ValidCodeRecord.class);
    }

    /**
     * 统计时间段内设备号短息发送量
     * @param star
     * @param end
     * @param imei
     * @return
     */
    public Long countValidImeiNum(LocalDateTime star, LocalDateTime end,String imei){
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("creatTime").gte(star).lte(end);
        criteria.and("imei").is(imei);
        query.addCriteria(criteria);
        return mongoTemplate.count(query, ValidCodeRecord.class);
    }

    /**
     * 统计时间段内ip地址短信发送量
     * @param star
     * @param end
     * @param ipAddr
     * @return
     */
    public Long countValidIpAddrNum(LocalDateTime star, LocalDateTime end,String ipAddr){
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("creatTime").gte(star).lte(end);
        criteria.and("ipAddr").is(ipAddr);
        query.addCriteria(criteria);
        return mongoTemplate.count(query, ValidCodeRecord.class);
    }




}
